﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Orders
{
	public partial class ePutThroughOrder : WeifenLuo.WinFormsUI.Docking.DockContent  
	{
        public ePutThroughOrder()
		{
			InitializeComponent();
		}
	
		private void eOrdersNotFound_Load(object sender, EventArgs e)
		{
			try
			{
                ABSCommonLibrary.SetSymbols(ref cboSymbol);

				ABSCommonLibrary.SetComboBoxControl(ref cboTransType,
                    new MyComboItem[3]{new MyComboItem("1F", "Cùng thành viên")
				,new MyComboItem("2S", "Bán khác thành viên")
				,new MyComboItem("2B", "Mua khác thành viên")});
				
			}
			catch (Exception ex)
			{
				ABSCommonLibrary.ShowErrorMessage(ex.ToString());
			}
		}
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                string sOrderText = cboTransType.Text.ToLower() + " " +
                    nbrVolume.Value.ToString(DeclareLibrary.CURRENCY_FORMAT) + " " +
                    cboSymbol.Text + " giá " + nbrPrice.Text;
                    
                if (ABSCommonLibrary.ShowQuestion(
                    "Bạn muốn đặt lệnh " + sOrderText + " ?") == DialogResult.Yes)
                {                    
                    object[] p_param = new object[11];                   
                    p_param[0] = ((MyComboItem)cboTransType.SelectedItem).ID;
                    p_param[1] = cboSymbol.Text;
                    p_param[2] = nbrVolume.Value;
                    p_param[3] = nbrPrice.Text;
                    p_param[4] = txtAccountNoBuyer.Value;
                    p_param[5] = txtAccountNoSeller.Value;
                    p_param[6] = txtFirmBuyer.Text;                    
                    p_param[7] = txtTradeIdBuyer.Text;                    
                    p_param[8] = txtDealId.Text;                  
                    p_param[9] = DeclareLibrary.USER_NAME;
                    p_param[10] = DeclareLibrary.MARKET_DAY;

                    int error_code = Convert.ToInt32(ABSCommonLibrary.DoExecute(
                        CommonLibrary.CommonLibrary.BOSC002006, ref p_param));
                    switch (error_code)
                    {
                        case -1:
                            ABSCommonLibrary.ShowMessage("Khong du khoi luong thoa thuan");
                            nbrVolume.Focus();
                            break;
                        case -2:
                            ABSCommonLibrary.ShowMessage("Chung khoan lua chon khong ton tai");
                            cboSymbol.Focus();
                            break;
                        case -3:
                            ABSCommonLibrary.ShowMessage("Chung khoan bi ngung giao dich");
                            cboSymbol.Focus();
                            break;
                        case -4:
                            ABSCommonLibrary.ShowMessage("Khong ton tai tai khoan mua");
                            txtAccountNoBuyer.Focus();
                            break;                        
                        case -5:
                            ABSCommonLibrary.ShowMessage("Khong dung tai khoan ban");
                            txtAccountNoSeller.Focus();
                            break;
                        case -6:
                            ABSCommonLibrary.ShowMessage("Khong dat lenh nguoc chieu");
                            cboTransType.Focus();
                            break;
                        case -7:
                            ABSCommonLibrary.ShowMessage("Khong du tien dat mua");
                            txtAccountNoBuyer.Focus();
                            break;
                        case -8:
                            ABSCommonLibrary.ShowMessage("Khong du chung khoan dat ban");
                            txtAccountNoSeller.Focus();
                            break;
                        case -9:
                            ABSCommonLibrary.ShowMessage("Khong co so deal xac nhan voi thoa thuan ben mua");
                            txtAccountNoSeller.Focus();
                            break;
                        case -10:
                            ABSCommonLibrary.ShowMessage("Khong dung gia tran san");
                            cboSymbol.Focus();
                            break;
                        case -11:
                            ABSCommonLibrary.ShowMessage("Khong du room cho tai khoan nuoc ngoai");
                            txtAccountNoBuyer.Focus();
                            break;
                        default:
                            ABSCommonLibrary.ShowMessage("Lenh dat " + sOrderText + " thanh cong");
                            break;
                    }

                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void cboTransType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (((MyComboItem)cboTransType.SelectedItem).ID)
            {
                case "1F":
                    labelDealId.Visible = false;
                    txtDealId.Visible = false;

                    labelClientSeller.Visible = true;
                    txtAccountNoSeller.Visible = true;

                    labelClientBuyer.Visible = true;
                    txtAccountNoBuyer.Visible = true;

                    txtFirmBuyer.Visible = false;
                    txtTradeIdBuyer.Visible = false;

                    labelFirmBuyer.Visible = false;
                    labelTradeBuyer.Visible = false;
                    break;
                case "2B":
                    labelClientSeller.Visible = false;
                    txtAccountNoSeller.Visible = false;
                    labelClientBuyer.Visible = true;
                    txtAccountNoBuyer.Visible = true;
                    txtFirmBuyer.Visible = false;
                    txtTradeIdBuyer.Visible = false;
                    labelDealId.Visible = true;
                    txtDealId.Visible = true;
                    labelFirmBuyer.Visible = false;
                    labelTradeBuyer.Visible = false;
                    break;

                case "2S":
                    labelClientSeller.Visible = true;
                    txtAccountNoSeller.Visible = true;
                    labelClientBuyer.Visible = false;
                    txtAccountNoBuyer.Visible = false;
                    labelFirmBuyer.Visible = true;
                    labelTradeBuyer.Visible = true;
                    txtFirmBuyer.Visible = true;
                    txtTradeIdBuyer.Visible = true;
                    labelDealId.Visible = false;
                    txtDealId.Visible = false;
                    break;
                default:
                    break;
            }
        }

        private void cboSymbol_Validated(object sender, EventArgs e)
        {
            DataRow[] rows = DeclareLibrary.SYMBOLS.Tables[0]
               .Select("CODE = '" + cboSymbol.Text.ToUpper() + "'");
            if (rows.Length > 0)
            {
                lblCePrice.Text = Convert.ToDecimal(rows[0]["upper_price"]).ToString(DeclareLibrary.CURRENCY_FORMAT);
                lblFlPrice.Text = Convert.ToDecimal(rows[0]["lower_price"]).ToString(DeclareLibrary.CURRENCY_FORMAT);

            }
            else
            {

                lblCePrice.ResetText();
                lblFlPrice.ResetText();
            }
        }
	}
}